home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / gengetopt-2.6.lha / gengetopt-2.6 / doc / cmdline1.c next >
Text File  |  2002-06-24  |  7KB  |  251 lines

  1. /*
  2.   File autogenerated by gengetopt version 2.6  
  3.   generated with the following command:
  4.   ../src/gengetopt -isample1.ggo -Fcmdline1 --long-help -u 
  5.  
  6.   The developers of gengetopt consider the fixed text that goes in all
  7.   gengetopt output files to be in the public domain:
  8.   we make no copyright claims on it.
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. /* If we use autoconf.  */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. /* Check for configure's getopt check result.  */
  20. #ifndef HAVE_GETOPT_LONG
  21. #include "getopt.h"
  22. #else
  23. #include <getopt.h>
  24. #endif
  25.  
  26. #ifndef HAVE_STRDUP
  27. #define strdup gengetopt_strdup
  28. #endif /* HAVE_STRDUP */
  29.  
  30. #include "cmdline1.h"
  31.  
  32.  
  33. void
  34. cmdline_parser_print_version (void)
  35. {
  36.   printf ("%s %s\n", PACKAGE, VERSION);
  37. }
  38.  
  39. void
  40. cmdline_parser_print_help (void)
  41. {
  42.   cmdline_parser_print_version ();
  43.   printf("\n"
  44. "Usage: %s -iINT|--int-opt=INT [-h|--help] [-V|--version] -sSTRING|--str-opt=STRING -mINT|--my-opt=INT [--flag-opt] [-F|--funct-opt] --long-opt=LONG --def-opt=STRING  [FILES]...\n", PACKAGE);
  45.   printf("   -h         --help            Print help and exit\n");
  46.   printf("   -V         --version         Print version and exit\n");
  47.   printf("   -sSTRING   --str-opt=STRING  A string option\n");
  48.   printf("   -mINT      --my-opt=INT      Another integer option\n");
  49.   printf("   -iINT      --int-opt=INT     A int option\n");
  50.   printf("              --flag-opt        A flag option (default=off)\n");
  51.   printf("   -F         --funct-opt       A function option\n");
  52.   printf("              --long-opt=LONG   A long option\n");
  53.   printf("              --def-opt=STRING  A string option with default (default='Hello')\n");
  54. }
  55.  
  56.  
  57. (null)
  58.  
  59.  
  60. Ì
  61.  
  62.  
  63. (null)
  64. 
  65.  
  66. Undefined error: 0
  67. Operation not permitted
  68.  
  69. int
  70. cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
  71. {
  72.   int c;    /* Character of the parsed option.  */
  73.   int missing_required_options = 0;    
  74.  
  75.   args_info->help_given = 0 ;
  76.   args_info->version_given = 0 ;
  77.   args_info->str_opt_given = 0 ;
  78.   args_info->my_opt_given = 0 ;
  79.   args_info->int_opt_given = 0 ;
  80.   args_info->flag_opt_given = 0 ;
  81.   args_info->funct_opt_given = 0 ;
  82.   args_info->long_opt_given = 0 ;
  83.   args_info->def_opt_given = 0 ;
  84. #define clear_args() { \
  85.   args_info->str_opt_arg = NULL; \
  86.   args_info->flag_opt_flag = 0;\
  87.   args_info->def_opt_arg = strdup("Hello") ;\
  88. }
  89.  
  90.   clear_args();
  91.  
  92.   args_info->inputs = NULL;
  93.   args_info->inputs_num = 0;
  94.  
  95.   optarg = 0;
  96.   optind = 1;
  97.   opterr = 1;
  98.   optopt = '?';
  99.  
  100.   while (1)
  101.     {
  102.       int option_index = 0;
  103.       static struct option long_options[] = {
  104.         { "help",    0, NULL, 'h' },
  105.         { "version",    0, NULL, 'V' },
  106.         { "str-opt",    1, NULL, 's' },
  107.         { "my-opt",    1, NULL, 'm' },
  108.         { "int-opt",    1, NULL, 'i' },
  109.         { "flag-opt",    0, NULL, 0 },
  110.         { "funct-opt",    0, NULL, 'F' },
  111.         { "long-opt",    1, NULL, 0 },
  112.         { "def-opt",    1, NULL, 0 },
  113.         { NULL,    0, NULL, 0 }
  114.       };
  115.  
  116.       c = getopt_long (argc, argv, "hVs:m:i:F", long_options, &option_index);
  117.  
  118.       if (c == -1) break;    /* Exit from `while (1)' loop.  */
  119.  
  120.       switch (c)
  121.         {
  122.         case 'h':    /* Print help and exit.  */
  123.           clear_args ();
  124.           cmdline_parser_print_help ();
  125.           exit (EXIT_SUCCESS);
  126.  
  127.         case 'V':    /* Print version and exit.  */
  128.           clear_args ();
  129.           cmdline_parser_print_version ();
  130.           exit (EXIT_SUCCESS);
  131.  
  132.         case 's':    /* A string option.  */
  133.           if (args_info->str_opt_given)
  134.             {
  135.               fprintf (stderr, "%s: `--str-opt' (`-s') option given more than once\n", PACKAGE);
  136.               clear_args ();
  137.               exit (EXIT_FAILURE);
  138.             }
  139.           args_info->str_opt_given = 1;
  140.           args_info->str_opt_arg = strdup (optarg);
  141.           break;
  142.  
  143.         case 'm':    /* Another integer option.  */
  144.           if (args_info->my_opt_given)
  145.             {
  146.               fprintf (stderr, "%s: `--my-opt' (`-m') option given more than once\n", PACKAGE);
  147.               clear_args ();
  148.               exit (EXIT_FAILURE);
  149.             }
  150.           args_info->my_opt_given = 1;
  151.           args_info->my_opt_arg = atoi (optarg);
  152.           break;
  153.  
  154.         case 'i':    /* A int option.  */
  155.           if (args_info->int_opt_given)
  156.             {
  157.               fprintf (stderr, "%s: `--int-opt' (`-i') option given more than once\n", PACKAGE);
  158.               clear_args ();
  159.               exit (EXIT_FAILURE);
  160.             }
  161.           args_info->int_opt_given = 1;
  162.           args_info->int_opt_arg = atoi (optarg);
  163.           break;
  164.  
  165.         
  166.         case 'F':    /* A function option.  */
  167.           if (args_info->funct_opt_given)
  168.             {
  169.               fprintf (stderr, "%s: `--funct-opt' (`-F') option given more than once\n", PACKAGE);
  170.               clear_args ();
  171.               exit (EXIT_FAILURE);
  172.             }
  173.           args_info->funct_opt_given = 1;
  174.           break;
  175.  
  176.         
  177.         
  178.         case 0:    /* Long option with no short option */
  179.           /* A flag option.  */
  180.           if (strcmp (long_options[option_index].name, "flag-opt") == 0)
  181.           {
  182.             if (args_info->flag_opt_given)
  183.               {
  184.                 fprintf (stderr, "%s: `--flag-opt' option given more than once\n", PACKAGE);
  185.                 clear_args ();
  186.                 exit (EXIT_FAILURE);
  187.               }
  188.             args_info->flag_opt_given = 1;
  189.             args_info->flag_opt_flag = !(args_info->flag_opt_flag);
  190.             break;
  191.           }
  192.           /* A long option.  */
  193.           else if (strcmp (long_options[option_index].name, "long-opt") == 0)
  194.           {
  195.             if (args_info->long_opt_given)
  196.               {
  197.                 fprintf (stderr, "%s: `--long-opt' option given more than once\n", PACKAGE);
  198.                 clear_args ();
  199.                 exit (EXIT_FAILURE);
  200.               }
  201.             args_info->long_opt_given = 1;
  202.             args_info->long_opt_arg = atol (optarg);
  203.             break;
  204.           }
  205.           /* A string option with default.  */
  206.           else if (strcmp (long_options[option_index].name, "def-opt") == 0)
  207.           {
  208.             if (args_info->def_opt_given)
  209.               {
  210.                 fprintf (stderr, "%s: `--def-opt' option given more than once\n", PACKAGE);
  211.                 clear_args ();
  212.                 exit (EXIT_FAILURE);
  213.               }
  214.             args_info->def_opt_given = 1;
  215.             args_info->def_opt_arg = strdup (optarg);
  216.             break;
  217.           }
  218.  
  219.         case '?':    /* Invalid option.  */
  220.           /* `getopt_long' already printed an error message.  */
  221.           exit (EXIT_FAILURE);
  222.  
  223.         default:    /* bug: option not considered.  */
  224.           fprintf (stderr, "%s: option unknown: %c\n", PACKAGE, c);
  225.           abort ();
  226.         } /* switch */
  227.     } /* while */
  228.  
  229.   if (! args_info->int_opt_given)
  230.     {
  231.       fprintf (stderr, "%s: `--int-opt' (`-i') option required\n", PACKAGE);
  232.       missing_required_options = 1;
  233.     }
  234.  
  235.   if ( missing_required_options )
  236.     exit (EXIT_FAILURE);
  237.  
  238.   if (optind < argc)
  239.     {
  240.       int i = 0 ;
  241.  
  242.       args_info->inputs_num = argc - optind ;
  243.       args_info->inputs = 
  244.         (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
  245.       while (optind < argc)
  246.         args_info->inputs[ i++ ] = strdup (argv[optind++]) ; 
  247.     }
  248.  
  249.   return 0;
  250. }
  251.